﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

namespace ClimateChangeChampionsCustomAdmin
{
    public partial class Schools : Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl
    {
        protected override void OnInit(EventArgs e)
        {
            dsUsers.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.User>>(dsUsers_SelectData);
            dsUsers.UpdatingData += new EventHandler<Acms.DataControls.UpdatingDataEventArgs<User>>(dsUsers_UpdatingData);
            base.OnInit(e);
        }

        void dsUsers_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<User> e)
        {
            Telerik.Web.UI.GridDataItem editItem = gridSchools.EditItems[0] as Telerik.Web.UI.GridDataItem;
            if(editItem != null)
            {
                TextBox txtSchoolName = editItem.EditFormItem.FindControl("txtSchoolName") as TextBox;
                if (txtSchoolName != null)
                {
                    if (e.Item.GetCustomProperty("School Name") != txtSchoolName.Text.Trim())
                    {
                        e.Item.SetCustomProperty("School Name", txtSchoolName.Text.Trim());
                    }
                }

                DropDownList ddlType = editItem.EditFormItem.FindControl("ddlType") as DropDownList;
                if (ddlType != null)
                {
                    if (e.Item.GetCustomProperty("School Type") != ddlType.SelectedValue)
                    {
                        e.Item.SetCustomProperty("School Type", ddlType.SelectedValue);
                    }
                }

                TextBox txtSchoolTel = editItem.EditFormItem.FindControl("txtSchoolTel") as TextBox;
                if (txtSchoolTel != null)
                {
                    if (e.Item.GetCustomProperty("School Tel") != txtSchoolTel.Text.Trim())
                    {
                        e.Item.SetCustomProperty("School Tel", txtSchoolTel.Text.Trim());
                    }
                }

                TextBox txtSecondaryContact = editItem.EditFormItem.FindControl("txtSecondaryContact") as TextBox;
                if (txtSecondaryContact != null)
                {
                    if (e.Item.GetCustomProperty("Secondary Contact Name") != txtSecondaryContact.Text.Trim())
                    {
                        e.Item.SetCustomProperty("Secondary Contact Name", txtSecondaryContact.Text.Trim());
                    }
                }
            }
            
        }

        void dsUsers_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.User> e)
        {
            List<User> users = Acms.Bll.User.Search(AcmsSite.Id);
            List<User> filtered = (from u in users where !string.IsNullOrEmpty(u.GetCustomProperty("School Name")) select u).ToList();
            Acms.Bll.User.Sort(filtered, gridSchools.MasterTableView.SortExpressions.GetSortString());
            e.SetData(filtered);
        }

        protected void download_FieldValueDataBound(object sender, Acms.WebControls.DownloadingGrid.FieldValueDataBoundEventArgs e)
        {
            User data = e.DataItem as User;
            if (data != null)
            {
                if (e.Field.HeaderText == "School Name")
                {
                    e.Value = data.GetCustomProperty("School Name");
                }
                else if (e.Field.HeaderText == "School Type")
                {
                    e.Value = data.GetCustomProperty("School Type");
                }
                else if (e.Field.HeaderText == "School Tel")
                {
                    e.Value = data.GetCustomProperty("School Tel");
                }
                else if (e.Field.HeaderText == "Secondary Contact Name")
                {
                    e.Value = data.GetCustomProperty("Secondary Contact Name");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

    }
}